package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketAddress;
import com.cskaoyan.market.service.WxAddressService;
import com.cskaoyan.market.util.ResponseUtil;
import javafx.geometry.Pos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author XuFeiLong
 * @DATE: 2023/4/2
 * @TIME: 0:07
 */
@RestController
@RequestMapping("wx/address")
public class WxAddressController {

    @Autowired
    WxAddressService addressService;

    /**
     * 获取地址列表
     *
     * @return
     */
    @GetMapping("list")
    public Object list() {
        List<MarketAddress> addressList = addressService.list();

        return ResponseUtil.okList(addressList);
    }

    @GetMapping("detail")
    public Object detail(Integer id) {
        MarketAddress result = addressService.detail(id);
        return ResponseUtil.ok(result);
    }

    @PostMapping("save")
    public Object save(@RequestBody MarketAddress address) {
        if (address.getTel().length() != 11) {
            return ResponseUtil.badArgumentValue();
        }
        // data 要返回save 后 address id的值
        int data = addressService.save(address);
        HashMap<String, Object> result = new HashMap<>();
        result.put("data", data);
        return ResponseUtil.ok(result);
    }

    @PostMapping("delete")
    public Object delete(@RequestBody Map<String, Integer> para) {
        Integer id = para.get("id");
        int code = addressService.delete(id);

        if (code == 200) {
            return ResponseUtil.ok();
        }
        return ResponseUtil.badArgument();
    }
}
